package com.cy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class ProviderApplication {
    /**
     * org.slf4j.Logger (Log4j,Logback都是基于slf4j规范实现的日志库)
     * getLogger(类名.class) 哪个类中输出的日志
     */
    private static final Logger logger = LoggerFactory.getLogger(ProviderApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }

    @Value("${server.port}")
    private String server;
    /**
     * @RefreshScope 注解
     * 作用:
     *      在配置中心的相关配置发生变化以后，能够及时看到更新
     *      底层是通过重新创建Controller对象的方式,对属性进行了重新初始化
     */
    @RefreshScope   //动态刷新配置
    @RestController
    public class ProviderController{
        @GetMapping(value = "/provider/echo/{msg}")
        public String doEcho(@PathVariable String msg) {
            if(msg==null||msg.length()<3) throw new IllegalArgumentException("参数不合法");
            return server+"say:Hello" + msg;
        }

        //读取配置中心的日志级别
        @Value("${logging.level.com.cy:error}") // ":" 后为level默认值
        private String logLevel;
        @GetMapping("/provider/doGetLogLevel")
        public String doGetLevel() {
            /*  日志优先级: error > warn > info > debug > trace  */
            logger.trace("==log.trace");    //跟踪
            logger.debug("==log.debug");    //调试
            logger.info("==log.info");      //常规信息
            logger.warn("==log.warn");      //警告
            logger.error("==log.error");    //错误信息
            return "log level is "+logLevel;
        }

        @Value("${server.tomcat.threads.max:200}")
        private Integer maxThread;
        @RefreshScope
        @RequestMapping("/provider/doGetMaxThread")
        public String doGetMaxThread(){
            return "server.threads.max is  "+ maxThread;
        }

        @Value("${page.pageSize:10}")
        private Integer pageSize;
        @GetMapping("/provider/doGetPageSize")
        public String doGetPageSize(){
            //return String.format()
            return "page size is "+pageSize;
        }
    }
}
